package com.bls.blsdemo.PermissionDemo;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.blankj.utilcode.util.LogUtils;
import com.bls.blsdemo.R;
import com.bls.blsdemo.utils.GlideImageLoader;
import com.bls.lib.dialog.ProgressDialog;
import com.bls.lib.permission.Permission;
import com.bls.lib.permission.PermissionResult;
import com.bls.lib.ui.activity.BaseActivity;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.lzy.imagepicker.view.CropImageView;

import java.util.ArrayList;

@SuppressWarnings("unchecked")
public class PermissionActivity extends BaseActivity {

    private Button btnHeader;
    private ImageView ivHeader;
    private Activity mActivity;
    private static final int IMAGE_PICKER = 100;
    private static final int PERMISSION_CAMERA = 110;
    private ImagePicker imagePicker;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_permission;
    }

    @Override
    public void findView() {
        btnHeader = findViewById(R.id.btn_header);
        ivHeader = findViewById(R.id.iv_header);
    }

    @Override
    protected void initMain() {
        mActivity = this;
        initImagePicker();
    }


    @Override
    protected void initView() {

    }

    @Override
    protected void initListener() {
        setOnClickLisenter(btnHeader);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_header:
                LogUtils.i("btn_header");
                EventBtnHeader();
               // new ProgressDialog(PermissionActivity.this).show();
                break;
        }
    }

    private void EventBtnHeader() {
        LogUtils.i("设置头像");
        Permission.checkPermission(mActivity, new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE}, new PermissionResult() {
            @Override
            public void success() {
                Intent intent = new Intent(mActivity, ImageGridActivity.class);
                mActivity.startActivityForResult(intent, IMAGE_PICKER);
            }

            @Override
            public void fail() {
                LogUtils.i("请求权限失败---------------------------");
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            if (data != null && requestCode == IMAGE_PICKER) {
                ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                if (images.get(0).path != null)
                    imagePicker.getImageLoader().displayImage(getActivity(), images.get(0).path, ivHeader, 500, 500);
            }
        }
    }

    private void initImagePicker() {
        imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new GlideImageLoader());
        imagePicker.setShowCamera(true);
        imagePicker.setCrop(true);
        imagePicker.setSaveRectangle(true); //是否按矩形区域保存
        imagePicker.setSelectLimit(1);    //选中数量限制
        imagePicker.setMultiMode(false);
        imagePicker.setStyle(CropImageView.Style.CIRCLE);  //裁剪框的形状
        imagePicker.setFocusWidth(800);   //裁剪框的宽度。单位像素（圆形自动取宽高最小值）
        imagePicker.setFocusHeight(800);  //裁剪框的高度。单位像素（圆形自动取宽高最小值）
        imagePicker.setOutPutX(1000);//保存文件的宽度。单位像素
        imagePicker.setOutPutY(1000);//保存文件的高度。单位像素
    }
}
